﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace Lavka.Core
{
    public static class FileHelper
    {
        public static Bitmap CropBitmap(Bitmap bitmap)
        {
                int CropSize = Math.Min(bitmap.Width, bitmap.Height);
                Rectangle rect = new Rectangle(0, 0, CropSize, CropSize);
                Bitmap cropped = bitmap.Clone(rect, bitmap.PixelFormat);
                return cropped;
        }
        public static bool CreateThumbnail(string filePath)
        {
            int lnWidth = 100;
            int lnHeight = 100;
            var path = HttpContext.Current.Server.MapPath(filePath);
            var thumbPath = Path.Combine(HttpContext.Current.Server.MapPath("~/upload/products/thumbnails/"), 
                Path.GetFileName(filePath));

            Bitmap bmpOut = null;
            try
            {
                Bitmap loBMP = new Bitmap(path);
                bmpOut = new Bitmap(lnWidth, lnHeight);
                Graphics g = Graphics.FromImage(bmpOut);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.FillRectangle(Brushes.White, 0, 0, lnWidth, lnHeight);
                g.DrawImage(CropBitmap(loBMP), 0, 0, lnWidth, lnHeight);
                loBMP.Dispose();
            }
            catch
            {
                return false;
            }
            var oldFile = new FileInfo(thumbPath);
            oldFile.Delete();

            bmpOut.Save(thumbPath, ImageFormat.Jpeg);
            return true;
        }
        public static string UploadPhoto(HttpPostedFileBase file, string catalog, int id)
        {
            if (file != null && file.ContentLength > 0)
            {
                // extract only the fielname
                var fileName = Path.GetFileName(file.FileName);
                var extention = Path.GetExtension(fileName);
                string newName = id.ToString() + extention;
                // store the file inside ~/App_Data/uploads folder
                var relativePath = Path.Combine("~/upload/" + catalog + "/", newName);
                var path = Path.Combine(HttpContext.Current.Server.MapPath("~/upload/" + catalog + "/"), newName);
                
                var oldFile = new FileInfo(path);
                oldFile.Delete();
                file.SaveAs(path);

                CreateThumbnail(relativePath);

                return relativePath;
            }
            if (HttpContext.Current.Request.Form["hPhoto"] != null)
            {
                return HttpContext.Current.Request.Form["hPhoto"];
            }
            return "";
        }
        public static string UploadPrice(HttpPostedFileBase file)
        {
            if (file != null && file.ContentLength > 0)
            {
                // extract only the fielname
                var fileName = Path.GetFileName(file.FileName);
                var extention = Path.GetExtension(fileName);
                string newName = "price" + extention;
                // store the file inside ~/App_Data/uploads folder
                var relativePath = Path.Combine("~/upload/temp/", newName);
                var path = Path.Combine(HttpContext.Current.Server.MapPath("~/upload/temp/"), newName);
                file.SaveAs(path);
                return File.ReadAllText(path, Encoding.GetEncoding(1251));;
            }
            else
            {
                throw new NoNullAllowedException("File is empty!");
            }
        }

    }
}